package com.zlf.service;

import com.zlf.mapper.WalletTransactionMapper;
import com.zlf.po.Wallet;
import com.zlf.po.WalletTransaction;
import com.zlf.util.WalletRules;
import com.zlf.mapper.WalletMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class WalletService {
    @Autowired
    private WalletMapper walletMapper;
    @Autowired
    private WalletTransactionMapper transactionMapper;
    public int createWallet(Long id,String pass) throws Exception {
        if(getWallet(id)!=null){
            throw new Exception("用户已存在！");
        }
        Wallet wallet=new Wallet(id,pass);
        return walletMapper.createWallet(wallet);
    }
    public Wallet getWallet(Long walletId){
        Wallet wallet=walletMapper.getWallet(walletId);
        return wallet;
    }
    public void updateWallet(Wallet wallet){
       walletMapper.updateWallet(wallet);
    }
    public void saveTransaction(WalletTransaction transaction){
        transactionMapper.saveTransaction(transaction);
    }
    public BigDecimal getBalance(Long walletId){
        Wallet wallet=getWallet(walletId);
        return wallet.balance();
    }
    public List<WalletTransaction> listTransactionByWalletId(Long WalletId){
        List<WalletTransaction> list=transactionMapper.listTransactionByWalletId(WalletId);
        return list;
    }
    public List<WalletTransaction> listTransactionByFromWalletId(Long fromWalletId){
        return transactionMapper.listTransactionByFromWalletId(fromWalletId);
    }
    public List<WalletTransaction> listTransactionByToWalletId(Long toWalletId){
        return transactionMapper.listTransactionByToWalletId(toWalletId);
    }
    public void transfer (Long fromWalletId, Long toWalletId, BigDecimal amount,String pass) throws Exception{
        WalletTransaction transaction=new WalletTransaction(fromWalletId,toWalletId,amount);
        transaction.tobeconfirm();
        Wallet fromWallet=getWallet(fromWalletId);
        if(!fromWallet.checkPass(pass)){
            throw new Exception("密码错误！");
        }
        Wallet toWallet=getWallet(toWalletId);
        fromWallet.debit(amount);
        toWallet.credit(amount);
        toWallet.freeze(amount);
        saveTransaction(transaction);
        updateWallet(fromWallet);
        updateWallet(toWallet);
    }

    public void confirmTransaction(Long transferId) throws Exception {
        WalletTransaction transaction=transactionMapper.getTransactionById(transferId);
        transaction.confirm();
        Wallet wallet=getWallet(transaction.getToWalletId());
        wallet.unfreeze(transaction.getTransactionAmount());
        updateWallet(wallet);
        transactionMapper.confirm(transferId);
    }

    public void recharge(Long toWalletId,BigDecimal amount,String pass) throws Exception {
        //充值满WalletRules.recharge_a元赠WalletRules.recharge_b元
        WalletTransaction transaction1=new WalletTransaction(null,toWalletId,amount);
        BigDecimal extraK=amount.divide(BigDecimal.valueOf(WalletRules.recharge_a),0, RoundingMode.DOWN);
        BigDecimal extraAmount=extraK.multiply(BigDecimal.valueOf(WalletRules.recharge_b));
        WalletTransaction transaction2=new WalletTransaction(null,toWalletId,extraAmount);
        Wallet wallet=getWallet(toWalletId);
        if(!wallet.checkPass(pass)){
            throw new Exception("密码错误！");
        }
        wallet.credit(amount.add(extraAmount));
        updateWallet(wallet);
        saveTransaction(transaction1);
        if(extraAmount.compareTo(BigDecimal.ZERO)>0){
        saveTransaction(transaction2);}
    }

    public BigDecimal withdraw(Long fromWalletId,BigDecimal amount,String pass) throws Exception {
        //WalletRules.withdraw_rate手续费收取率
        WalletTransaction transaction=new WalletTransaction(fromWalletId,null,amount);
        Wallet wallet=getWallet(fromWalletId);
        if(!wallet.checkPass(pass)){
            throw new Exception("密码错误！");
        }
        if(wallet.isOverdraft()){
            throw new Exception("余额不足！");
        }
        wallet.debit(amount);
        updateWallet(wallet);
        saveTransaction(transaction);
        return amount.multiply(BigDecimal.valueOf(WalletRules.withdraw_rate)).setScale(2,RoundingMode.DOWN);
    }
    public void getVip(Long walletId,String pass) throws Exception {
        //消费满WalletRules.vip_a元增加WalletRules.vip_b元贷款额度
        Wallet wallet=getWallet(walletId);
        if(!wallet.checkPass(pass)){
            throw new Exception("密码错误！");
        }
        wallet.openOverdraft();
        BigDecimal consumption=transactionMapper.getConsumption(walletId);
        BigDecimal vipK=consumption.divide(BigDecimal.valueOf(WalletRules.vip_a),0, RoundingMode.DOWN);
        BigDecimal vipAmount=vipK.multiply(BigDecimal.valueOf(WalletRules.vip_b));
        wallet.increaseOverdraftAmount(vipAmount.subtract(wallet.overdraftAmount()));
        updateWallet(wallet);
    }

}
